using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay
{
    public class UIMainFormation : UIBaseWindow
    {
        #region Template Generate,don't modify
        protected class UIB_UIMainFormation
        {
            #region ObjectBinding Generate 
            public UnityEngine.GameObject heroUnit { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.GameObject>("heroUnit", out var __tbv0);
                this.heroUnit = __tbv0;
            }


            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region Window Config
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIMainFormation),
            layer = EUILayer.Main,
            permanent = true,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;
        #endregion Window Config

        #region fields

        private ListScrollAdapter<UIN_MainFormationHeroHead> forms;

        private FormationData formData;
        private List<FormationHeroData> formHeroDatas = new();

        #endregion fields

        #region properties

        protected UIB_UIMainFormation ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIMainFormation();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            forms = InitListScroll<UIN_MainFormationHeroHead>(this.ui.heroUnit, OnFormChanged);
        }

        protected override void OnShow(UIBaseData data)
        {
            SetData();
            ShowView();
        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {

        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {

        }

        #region methonds
        private void SetData()
        {
            formData = GameDataCenter.PlayerData.formation.defaultFormationData;
            formHeroDatas.Clear();

            foreach (var item in formData.heros)
            {
                if (item.Value != null && item.Value.heroData != null)
                {
                    formHeroDatas.Add(item.Value);
                }
            }

            if (formHeroDatas.Count > 1)
                formHeroDatas.Sort(Comparison);
        }

        int Comparison(FormationHeroData x, FormationHeroData y)
        {
            if (formData.captainHeroCid == x.cid) return -1;
            if (formData.captainHeroCid == y.cid) return 1;
            return x.pos - y.pos;
        }

        private void ShowView()
        {
            ShowForm();
        }

        private void ShowForm()
        {
            forms.RefillCells(formHeroDatas.Count);
        }

        private void OnFormChanged(UIN_MainFormationHeroHead item, int index)
        {
            item.Show(formHeroDatas[index]);
        }
        #endregion methonds
    }
}
